<?php

class Model_Form_Registration extends Model_Form
{
    public function init()
    {
        parent::init();

        $this
            ->setAction('/user/registration/submit')
            ->setMethod('post')
            ->setAttrib('id', 'registration-form')
            ->setAttrib('enctype', 'multipart/form-data');

        $this->addElements(array(
                $this->getUsername(),
                $this->getPassword(),
                $this->getPasswordConfirmation(),
                $this->getAvatar(),
                $this->getEmail(),
                $this->getSex(),
                $this->getBirthdate(),
                $this->getFullName(),
                $this->getAddress(),
                $this->getPostCode(),
                $this->getCity(),
                $this->getCountry(),
                $this->getPhone(),
                $this->getCaptcha(),
                $this->getSubmitButton()
            ));
    }

    private function getUsername()
    {
        $user = new Model_User();
        $el = $this->createElement('text', 'username');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Username:')
            ->setRequired(true)
            ->addFilter('StringToLower')
            ->addValidator('Alnum', true)
            ->addValidator('StringLength', true, array(1, 24))
            ->addValidator('DbUnique', true, array($user, 'username'));

        return $el;
    }

    private function getPassword()
    {
        $el = $this->createElement('password', 'password');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Password:')
            ->setRequired(true)
            ->addValidator('StringLength', false, array(6, 40));

        return $el;
    }

    private function getPasswordConfirmation()
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $el = $this->createElement('password', 'password_confirmation');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Password confirmation:')
            ->setRequired(true)
            ->addValidator('Identical',
                false,
                array($request->getParam('password')));

        return $el;
    }

    // File is renamed to the config's avatar path in the controller
    // Indeed, rename can only be done after upload
    private function getAvatar()
    {
        $el = $this->createElement('file', 'avatar');
        $el
            ->setDecorators($this->fileDecorators)
            ->setLabel('Upload an avatar:')
            ->setRequired(false)
            ->addValidator('Count', true, 1)
            ->addValidator('Size', true, 102400)
            ->addValidator('MimeType', true,
                array('image/gif', 'image/jpeg', 'image/png'))
            ->addValidator('Extension', true, array('gif', 'jpg', 'png'));

        return $el;
    }

    private function getSex()
    {
        $el = $this->createElement('radio', 'sex');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Sex:')
            ->setRequired(true)
            ->addValidator('Regex', false, array('`^[m|f]$`'))
            ->addMultiOptions(array('m' => 'male', 'f' => 'female'));
        $el
            ->getValidator('Regex')
            ->setMessage('The submitted value is neither \'m\' nor \'f\'');

        return $el;
    }

    private function getBirthdate()
    {
        $el = $this->createElement('text', 'birthdate');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Birthdate:')
            ->setRequired(true)
            ->addValidator('Date');

        return $el;
    }

    private function getFullName()
    {
        $el = $this->createElement('text', 'full_name');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Full name:')
            ->setRequired(true)
            ->addValidator('StringLength', false, array(1, 50));

        return $el;
    }

    private function getAddress()
    {
        $el = $this->createElement('text', 'address');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Address:')
            ->setRequired(true)
            ->addValidator('StringLength', false, array(1, 50));

        return $el;
    }

    private function getPostCode()
    {
        $el = $this->createElement('text', 'postcode');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Postcode:')
            ->setRequired(true)
            ->addValidator('StringLength', false, array(1, 15));

        return $el;
    }

    private function getCity()
    {
        $el = $this->createElement('text', 'city');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('City:')
            ->setRequired(true)
            ->addValidator('StringLength', false, array(1, 50));

        return $el;
    }

    private function getCountry()
    {
        $el = $this->createElement('select', 'country');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Country:')
            ->setRequired(true)
            ->addValidator('Int')
            ->addMultiOptions($this->getCountries());

        return $el;
    }

    private function getPhone()
    {
        $el = $this->createElement('text', 'phone');
        $el
            ->setDecorators($this->elementDecorators)
            ->setLabel('Phone:')
            ->addValidator('StringLength', false, array(1, 50));

        return $el;
    }

    private function getCaptcha()
    {
        $el = new Zend_Form_Element_Captcha('captcha', array(
            'label' => 'Please verify you\'re a human:',
            'captcha' => 'Image',
            'captchaOptions' => array(
                'captcha' => 'Image',
                'wordLen' => 5,
                'expiration' => 310,
                'gcFreq' => 50,
                'font' => Zend_Registry::get('config')
                                ->paths
                                ->library . '/Fonts/FreeSans.ttf',
                'fontSize' => '24',
                'width' => 150,
                'height' => 50,
                'timeout' => 300,
                'imgDir' => Zend_Registry::get('config')
                                ->paths
                                ->public . '/images/captchas/',
                'imgUrl' => '/images/captchas/',
            ),
        ));

        return $el;
    }

    private function getCountries()
    {
        return array
        (
            0 => '',
            1 => 'Afghanistan',
            2 => 'Åland Islands',
            3 => 'Albania',
            4 => 'Algeria',
            5 => 'American Samoa',
            6 => 'Andorra',
            7 => 'Angola',
            8 => 'Anguilla',
            9 => 'Antigua and Barbuda',
            10 => 'Argentina',
            11 => 'Armenia',
            12 => 'Aruba',
            13 => 'Australia',
            14 => 'Austria',
            15 => 'Azerbaijan',
            16 => 'Bahamas',
            17 => 'Bangladesh',
            18 => 'Barbados',
            19 => 'Belarus',
            20 => 'Belgium',
            21 => 'Belize',
            22 => 'Benin',
            23 => 'Bermuda',
            24 => 'Bhutan',
            25 => 'Bolivia',
            26 => 'Bosnia and Herzegovina',
            27 => 'Botswana',
            28 => 'Bouvet Island',
            29 => 'Brazil',
            30 => 'British Indian Ocean Territory',
            31 => 'Brunei Darussalam',
            32 => 'Bulgaria',
            33 => 'Burkina Faso',
            34 => 'Burundi',
            35 => 'Cambodia',
            36 => 'Cameroon',
            37 => 'Canada',
            38 => 'Cape Verde',
            39 => 'Cayman Islands',
            40 => 'Central African Republic',
            41 => 'Chad',
            42 => 'Chile',
            43 => 'China',
            44 => 'Christmas Island',
            45 => 'Cocos (Keeling) Islands',
            46 => 'Colombia',
            47 => 'Comoros',
            48 => 'Congo',
            49 => 'Congo, Democratic Republic of the',
            50 => 'Cook Islands',
            51 => 'Costa Rica',
            52 => 'Côte d\'Ivoire',
            53 => 'Croatia',
            54 => 'Cuba',
            55 => 'Cyprus',
            56 => 'Czech Republic',
            57 => 'Djibouti',
            58 => 'Dominica',
            59 => 'Dominican Republic',
            60 => 'Ecuador',
            61 => 'Egypt',
            62 => 'El Salvador',
            63 => 'Equatorial Guinea',
            64 => 'Eritrea',
            65 => 'Estonia',
            66 => 'Ethiopia',
            67 => 'Falkland Islands (Malvinas)',
            68 => 'Faroe Islands',
            69 => 'Fiji',
            70 => 'Finland',
            71 => 'France',
            72 => 'French Guiana',
            73 => 'French Polynesia',
            74 => 'French Southern Territories',
            75 => 'Gabon',
            76 => 'Georgia',
            77 => 'Germany',
            78 => 'Ghana',
            79 => 'Gibraltar',
            80 => 'Greece',
            81 => 'Greenland',
            82 => 'Grenada',
            83 => 'Guadeloupe',
            84 => 'Guam',
            85 => 'Guatemala',
            86 => 'Guernsey',
            87 => 'Guinea',
            88 => 'Guinea-Bissau',
            89 => 'Guyana',
            90 => 'Haiti',
            91 => 'Holy See (Vatican City State)',
            92 => 'Honduras',
            93 => 'Hong Kong',
            94 => 'Hungary',
            95 => 'Iceland',
            96 => 'India',
            97 => 'Indonesia',
            98 => 'Iran, Islamic Republic of',
            99 => 'Iraq',
            100 => 'Ireland',
            101 => 'Isle of Man',
            102 => 'Israel',
            103 => 'Italy',
            104 => 'Japan',
            105 => 'Jersey',
            106 => 'Jordan',
            107 => 'Kazakhstan',
            108 => 'Kenya',
            109 => 'Kiribati',
            110 => 'Korea, Democratic People\'s Republic of',
            111 => 'Korea, Republic of',
            112 => 'Kuwait',
            113 => 'Kyrgyzstan',
            114 => 'Lao People\'s Democratic Republic',
            115 => 'Latvia',
            116 => 'Lebanon',
            117 => 'Lesotho',
            118 => 'Liberia',
            119 => 'Libyan Arab Jamahiriya',
            120 => 'Liechtenstein',
            121 => 'Lithuania',
            122 => 'Luxembourg',
            123 => 'Macedonia, the former Yugoslav Republic of',
            124 => 'Madagascar',
            125 => 'Malawi',
            126 => 'Malaysia',
            127 => 'Maldives',
            128 => 'Mali',
            129 => 'Malta',
            130 => 'Marshall Islands',
            131 => 'Martinique',
            132 => 'Mauritania',
            133 => 'Mauritius',
            134 => 'Mayotte',
            135 => 'Mexico',
            136 => 'Micronesia, Federated States of',
            137 => 'Moldova, Republic of',
            138 => 'Monaco',
            139 => 'Mongolia',
            140 => 'Montenegro',
            141 => 'Montserrat',
            142 => 'Morocco',
            143 => 'Mozambique',
            144 => 'Myanmar',
            145 => 'Namibia',
            146 => 'Nauru',
            147 => 'Nepal',
            148 => 'Netherlands',
            149 => 'Netherlands Antilles',
            150 => 'New Caledonia',
            151 => 'New Zealand',
            152 => 'Nicaragua',
            153 => 'Niger',
            154 => 'Nigeria',
            155 => 'Niue',
            156 => 'Norfolk Island',
            157 => 'Northern Mariana Islands',
            158 => 'Norway',
            159 => 'Oman',
            160 => 'Pakistan',
            161 => 'Palau',
            162 => 'Palestinian Territory, Occupied',
            163 => 'Panama',
            164 => 'Papua New Guinea',
            165 => 'Paraguay',
            166 => 'Peru',
            167 => 'Philippines',
            168 => 'Pitcairn',
            169 => 'Poland',
            170 => 'Portugal',
            171 => 'Puerto Rico',
            172 => 'Qatar',
            173 => 'Réunion',
            174 => 'Russian Federation',
            175 => 'Rwanda',
            176 => 'Saint Barthélemy',
            177 => 'Saint Helena',
            178 => 'Saint Kitts and Nevis',
            179 => 'Saint Lucia',
            180 => 'Saint Martin (French part)',
            181 => 'Saint Pierre and Miquelon',
            182 => 'Saint Vincent and the Grenadines',
            183 => 'Samoa',
            184 => 'San Marino',
            185 => 'Sao Tome and Principe',
            186 => 'Saudi Arabia',
            187 => 'Senegal',
            188 => 'Serbia',
            189 => 'Seychelles',
            190 => 'Sierra Leone',
            191 => 'Singapore',
            192 => 'Slovakia',
            193 => 'Slovenia',
            194 => 'Solomon Islands',
            195 => 'Somalia',
            196 => 'South Africa',
            197 => 'South Georgia and the South Sandwich Islands',
            198 => 'Spain',
            199 => 'Sri Lanka',
            200 => 'Sudan',
            201 => 'Suriname',
            202 => 'Svalbard and Jan Mayen',
            203 => 'Swaziland',
            204 => 'Sweden',
            205 => 'Switzerland',
            206 => 'Syrian Arab Republic',
            207 => 'Taiwan, Province of China',
            208 => 'Tanzania, United Republic of',
            209 => 'Thailand',
            210 => 'Timor-Leste',
            211 => 'Togo',
            212 => 'Tokelau',
            213 => 'Tonga',
            214 => 'Trinidad and Tobago',
            215 => 'Tunisia',
            216 => 'Turkey',
            217 => 'Turkmenistan',
            218 => 'Turks and Caicos Islands',
            219 => 'Tuvalu',
            220 => 'Ukraine',
            221 => 'United Arab Emirates',
            222 => 'United Kingdom',
            223 => 'United States',
            224 => 'United States Minor Outlying Islands',
            225 => 'Uruguay',
            226 => 'Uzbekistan',
            227 => 'Vanuatu',
            228 => 'Venezuela',
            229 => 'Viet Nam',
            230 => 'Virgin Islands, British',
            231 => 'Virgin Islands, U.S.',
            232 => 'Wallis and Futuna',
            233 => 'Yemen',
            234 => 'Zambia',
            235 => 'Zimbabwe',
        );
    }
}
